<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Dealer extends Access_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('crud_model');
		$this->load->model('dealer_model');
	}

	function index()
	{
		$page["_contents"] = "administrator/dealer/v_dealer";

		$data["query_prov"] = $this->dealer_model->getProvince();

		$this->template->load("administrator/template/main_template",$page,$data);
	}

	function load_list()
	{
		$arr['data'] = array();
		$query = $this->dealer_model->getAll();
		
		foreach($query->result() as $row)
		{
			$data = array();
			$link = "<a href='#' class='btn yellow edit-button' title='Edit Data' onclick='editData(" . $row->id . ")'><i class='fa fa-pencil'></i></a>";
			$link .= "<a href='#' class='btn red delete-button' title='Delete Data' onclick='deleteData(" . $row->id . ")'><i class='fa fa-minus-circle'></i></a>";
			array_push($data,$row->dealer_name);
			array_push($data,$row->address);
			array_push($data,$link);
			
			array_push($arr['data'],$data);
		}		
		//echo $car_id;
		echo json_encode($arr);
	}

	function add()
	{
		$data["name"] = $this->input->post("name");
		$data["id_prov"] = $this->input->post("id_prov");
		$data["id_kab"] = $this->input->post("id_kab");
		$data["address"] = $this->input->post("address");

		$this->crud_model->save('dealer',$data);
	}

	function edit()
	{
		$id = $this->uri->segment(4);
		
		$row = $this->dealer_model->getByID($id)->row();
		
		$arr = array(array('id' => $id, 
						   'name' => $row->name,
						   'id_prov' => $row->id_prov,
						   'id_kab' => $row->id_kab,
						   'address' => $row->address
						   )
					);
		echo json_encode($arr );
	}

	function update()
	{
		$id = $this->input->post("id");;
		$data["name"] = $this->input->post("name");
		$data["address"] = $this->input->post("address");
		$data["id_prov"] = $this->input->post("id_prov");
		$data["id_kab"] = $this->input->post("id_kab");
		
		$this->crud_model->update('dealer', $id, $data, 'id');
	}

	function delete()
	{
		$id = $this->input->post('id');

		$this->crud_model->delete('dealer', $id, 'id');
	}

	function load_kabupaten()
	{
		$id_prov = $this->input->post('id_prov');

		$q = $this->dealer_model->getKabupaten($id_prov);
		echo '<option value="">- Select City -</option>';
		foreach($q->result() as $row)
		{
			echo "<option value='". $row->id ."'>". $row->nama ."</option>";
		}
		exit;
	}

}

/* End of file user.php */
/* Location: ./application/controllers/administrator/user.php */